package cn.bluemobi.dylan.step.step.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.IBinder;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.bluemobi.dylan.step.activity.TiaoSheng;

public class TiaoShengService extends Service  implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor sensor;
    private double original_value;
    private double last_value;
    private double current_value;
    private boolean motionState=true;
    Thread thread;
    private static volatile boolean exit = false;


    private TiaoSheng tiaoSheng;

    public TiaoShengService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        thread = new Thread(new Runnable() {
            public void run() {
                startTiaoShengDetector();
            }
        });
        if(!exit) {
            thread.start();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }


    private void startTiaoShengDetector(){
        sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
        sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //获取传感器，在计步器中需要使用的是加速度传感器
        sensorManager.registerListener(this,sensor,sensorManager.SENSOR_DELAY_UI);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        tiaoSheng.button_start.setText(tiaoSheng.b);
        tiaoSheng.button_start1.setText(tiaoSheng.b1);
        double range=5;
        float[] value=event.values;
        current_value =value[2];
        if(motionState==true){
            if (current_value >= last_value)
                last_value = current_value;
            else {
                if(Math.abs(current_value-last_value)>range){
                    original_value=current_value;
                    motionState=false;
                }
            }
        }
        //向下加速的状态
        if(motionState==false){
            if (current_value <= last_value)
                last_value = current_value;
            else {
                //检测到一次峰值
                if(Math.abs(current_value-last_value)>range){
                    original_value=current_value;
                    if (tiaoSheng.processState==true){
                        tiaoSheng.step++; //检测到开始记录，步数加1
                        if(tiaoSheng.processState==true){
                            tiaoSheng.textView_step.setText(tiaoSheng.step+""); //更新读数
                        }
                    }
                    motionState=true;
                }
            }
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        exit = true;
    }



}